/* 
 * Author: k4ge
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Please contact k4ge at karthikjain@gmail.com or
 * visit www.karthikjain.wordpress.com if you need additional
 * information or have any questions.
 */

package myExperiments.multiClientServer;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

class S1 implements Runnable {
	ServerSocket ss;
	Socket cs;

	/**
	 * @param i
	 * @throws IOException
	 */
	public S1(int i) throws IOException {
		this.ss = new ServerSocket(i);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		int i = 0;
		while (true) {
			try {
				cs = ss.accept();
			} catch (IOException e) {
				System.out.println(e.toString());
			} catch (Exception e) {
				System.out.println(e.toString());
			}
			String name = "Client" + ++i;
			new Thread(new S2(cs), name).start();
		}
	}
}

class S2 implements Runnable {
	Socket cs;
	DataInputStream dis;
	DataOutputStream dos;

	float a, b, c, res;

	/**
	 * @param cs
	 */
	public S2(Socket cs) {
		this.cs = cs;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		System.out.println(Thread.currentThread().getName() + ": connected");
		try {
			dis = new DataInputStream(cs.getInputStream());

			a = dis.readFloat();
			b = dis.readFloat();
			c = dis.readFloat();
			res = (a + b + c) / 3;

			dos = new DataOutputStream(cs.getOutputStream());

			if (a < 0 || b < 0 || c < 0 || a > 100 || b > 100 || c > 100)
				dos.writeUTF("[error: Invalid marks entered]");
			else {
				if (res >= 90)
					dos.writeUTF("First Class with Distinction.");
				else if (res >= 70)
					dos.writeUTF("First Class.");
				else if (res >= 60)
					dos.writeUTF("Second Class.");
				else if (res >= 35)
					dos.writeUTF("Pass.");
				else
					dos.writeUTF("Fail.");
			}

			dis.close();
			dos.close();
			cs.close();
		} catch (IOException e) {
			System.out.println(e.toString());
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		System.out.println(Thread.currentThread().getName() + ": disconnected");
	}
}

public class MyServer {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Thread s1;
		try {
			s1 = new Thread(new S1(1947), "dispatcher");
			System.out.println(s1.getName() + ": service ready.");
			s1.start();
			System.out.println(s1.getName() + ": service started.");
		} catch (IOException e) {
			// e.printStackTrace();
			System.out.println(e.toString());
		}

	}
}
